package com.example.demo.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Rocket
 * @version 10.0.1
 * @date 2021-08-29 10:11 AM
 */
public class CalculatorLoggingHandler implements InvocationHandler {

    private Object cal;

    public void setCal(Object cal) {
        this.cal = cal;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] objects) throws Throwable {
        System.out.println("START -- ");
        Object result = method.invoke(cal, objects);
        System.out.println("Result is : " + result);
        System.out.println("END --");
        return result;
    }

    public static Object createProxy(Object target) {
        CalculatorLoggingHandler handler = new CalculatorLoggingHandler();
        handler.setCal(target);
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), handler);
    }
}
